import { Message, MessageBox } from 'element-ui'

export default {
  msg(type, val, s = 3000) {
    const tArr = ['success', 'error', 'info', 'warning']
    return Message({
      type: tArr[type],
      message: val,
      duration: s,
      showClose: !s,
    })
  },
  // s:显示时间
  msgSuc(val, s) {
    return this.msg(0, val, s)
  },
  msgErr(val, s) {
    return this.msg(1, val, s)
  },
  msgInf(val, s) {
    return this.msg(2, val, s)
  },
  msgWarn(val, s) {
    return this.msg(3, val, s)
  },
  
  alert(val, tit = '提示') {
    return MessageBox(String(val), tit)
  },
  
  showErr(val, title = '错误提示') {
    return MessageBox({
      type: 'error',
      title,
      message: String(val),
    })
  },
  
  confirm(content, title = '请确认', opt = {}) {
    return new Promise((resolve) => {
      MessageBox.confirm(content, title, opt).then(() => {
        resolve(true)
      }).catch(() => {
        resolve(false)
      })
    })
  },
  
  prompt(val, title = '提示') {
    return new Promise((resolve) => {
      MessageBox.prompt(val, title).then((res) => {
        resolve(res)
      }).catch(() => {
        resolve({})
      })
    })
  },
}
